import 'package:flutter/material.dart';

class YButton extends StatefulWidget {
  final String text;
  final VoidCallback? onPressed;
  final Color? backgroundColor;
  final Color? textColor;
  final Color? disabledColor;
  final Color? pressedColor;
  final double borderRadius;
  final EdgeInsetsGeometry padding;
  final double elevation;
  final bool isFullWidth;
  final bool isLoading;
  final Widget? prefixIcon;
  final Widget? suffixIcon;
  final TextStyle? textStyle;
  final BorderSide? borderSide;
  final Duration animationDuration;

  const YButton({
    Key? key,
    required this.text,
    required this.onPressed,
    this.backgroundColor,
    this.textColor,
    this.disabledColor,
    this.pressedColor,
    this.borderRadius = 8.0,
    this.padding = const EdgeInsets.symmetric(vertical: 6, horizontal: 6),
    this.elevation = 2.0,
    this.isFullWidth = false,
    this.isLoading = false,
    this.prefixIcon,
    this.suffixIcon,
    this.textStyle,
    this.borderSide,
    this.animationDuration = const Duration(milliseconds: 100),
  }) : super(key: key);

  @override
  _CustomButtonState createState() => _CustomButtonState();
}

class _CustomButtonState extends State<YButton> {
  bool _isPressed = false;

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    final bgColor = widget.backgroundColor ?? theme.primaryColor;
    final textColor = widget.textColor ?? theme.colorScheme.onPrimary;
    final disabledColor = widget.disabledColor ?? Colors.grey;
    final pressedColor = widget.pressedColor ?? bgColor.withOpacity(0.8);

    return GestureDetector(
      onTapDown: (_) => setState(() => _isPressed = true),
      onTapUp: (_) => setState(() => _isPressed = false),
      onTapCancel: () => setState(() => _isPressed = false),
      onTap: widget.onPressed,
      child: AnimatedContainer(
        duration: widget.animationDuration,
        width: widget.isFullWidth ? double.infinity : null,
        padding: widget.padding,
        decoration: BoxDecoration(
          color: widget.onPressed == null
              ? disabledColor
              : _isPressed
              ? pressedColor
              : bgColor,
          borderRadius: BorderRadius.circular(widget.borderRadius),
          border: widget.borderSide != null
              ? Border.fromBorderSide(widget.borderSide!)
              : null,
          // boxShadow: [
          //   if (widget.elevation > 0 && widget.onPressed != null && !_isPressed)
          //     BoxShadow(
          //       color: Color.fromRGBO(0, 0, 0, 0.1),
          //       blurRadius: widget.elevation,
          //       offset: Offset(0, widget.elevation / 2),
          //     ),
          // ],
        ),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          mainAxisSize: MainAxisSize.min,
          children: [
            if (widget.prefixIcon != null) ...[
              widget.prefixIcon!,
              const SizedBox(width: 8),
            ],
            widget.isLoading
                ? SizedBox(
              width: 20,
              height: 20,
              child: CircularProgressIndicator(
                strokeWidth: 2,
                valueColor: AlwaysStoppedAnimation<Color>(textColor),
              ),
            )
                : Text(
              widget.text,

              style: widget.textStyle ??
                  TextStyle(
                    color: textColor,
                    fontSize: 16,
                    fontWeight: FontWeight.w500,
                    decoration: TextDecoration.none,
                  ),
            ),
            if (widget.suffixIcon != null) ...[
              const SizedBox(width: 8),
              widget.suffixIcon!,
            ],
          ],
        ),
      ),
    );
  }
}